
//
// 120325 - AH - (Garage *) Created.
// 120626a- AH - (Huset *) Removed commented away code.
// 120626b- AH - (Huset *) Took in use ACCOUNT_ID. 
//

package org.soekrat.server.servlets.ce;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.Account;
import org.soekrat.server.io.db.ConversationElement;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements request to delete a
 *   {@link ConversationElement}s for a given account.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class DeleteConversationElementsHttpServlet
  extends AbstractSoekratHttpServlet
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DeleteConversationElementsHttpServlet(){}

  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
    }
    else
    {
      if (!validate(res, def, req)) return;
      
      long key=def.fetch(ACCOUNT_ID.name(), req).getLongValue();
      
      SoekratDAO dao=new SoekratDAOFactory().create();
        
      Account account=dao.getAccount(key);
      if (account==null)
      {
        respond(res, "Account for id: "+key+" not found");
      }
      else
      {
        dao.deleteConversationElements(account);
        respond(res, "ok");
      }
    }
  }
  
  @Override
  public HttpRequestDef getHttpRequestDef() 
  {
    return new HttpRequestDef("/soekrat_server/delete_ces")
      .desc("Delete Conversation Elements For Given Account")
      .parameter(ACCOUNT_ID);
  }
  
}
